// interface StorageData<T = any> {
//   data: T
//   expire: number | null
// }

/**
 *
 * @param {*} options { expire?: number | null } 单位秒
 * @returns
 */
export function createLocalStorage(options) {
  const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7;

  const { expire } = Object.assign({ expire: DEFAULT_CACHE_TIME }, options);

  function set(key, data) {
    const storageData = {
      data,
      expire: expire !== null ? new Date().getTime() + expire * 1000 : null
    };

    const json = JSON.stringify(storageData);
    localStorage.setItem(key, json);
  }

  function get(key) {
    const json = localStorage.getItem(key);
    if (json) {
      let storageData = null;

      try {
        storageData = JSON.parse(json);
      } catch (error) {
        console.log(error);
      }

      if (storageData) {
        const { data, expire } = storageData;
        if (expire === null || expire >= Date.now()) { return data; }
      }

      remove(key);
      return null;
    }
  }

  function remove(key) {
    localStorage.removeItem(key);
  }

  function clear() {
    localStorage.clear();
  }

  return { set, get, remove, clear };
}

export const ls = createLocalStorage();

export const ss = createLocalStorage({ expire: null });
